/*****初始化加载*****/
$(document).ready(function(){
    $('.preloader-it > .la-anim-1').addClass('la-animate');
    grandin();
    dropDrow.getDropDrown();
});
/*****Ready function end*****/

/*****加载函数启动*****/
$(window).on("load",function(){
    //渐变效果
    $(".preloader-it").delay(500).fadeOut("slow");
    /*进度条动画*/
    var progressAnim = $('.progress-anim');
    if( progressAnim.length > 0 ){
        for(var i = 0; i < progressAnim.length; i++){
            var $this = $(progressAnim[i]);
            $this.waypoint(function() {
                var progressBar = $(".progress-anim .progress-bar");
                for(var i = 0; i < progressBar.length; i++){
                    $this = $(progressBar[i]);
                    $this.css("width", $this.attr("aria-valuenow") + "%");
                }
            }, {
                triggerOnce: true,
                offset: 'bottom-in-view'
            });
        }
    }
});
/*****Load function* end*****/

/***** 高度设置 *****/
var setHeightWidth = function () {
    var height = $(window).height();
    var width = $(window).width();
    $('.full-height').css('height', (height));
    $('.page-wrapper').css('min-height', (height));
};

var $wrapper = $(".wrapper");
var grandin = function(){

    /*Counter Animation*/
    // var counterAnim = $('.counter-anim');
    // if( counterAnim.length > 0 ){
    //     counterAnim.counterUp({ delay: 10,
    //         time: 1000});
    // }

    /*提示框*/
    if( $('[data-toggle="tooltip"]').length > 0 )
        $('[data-toggle="tooltip"]').tooltip();

    /*弹出框*/
    if( $('[data-toggle="popover"]').length > 0 )
        $('[data-toggle="popover"]').popover()

    /*侧栏折叠动画*/
    var sidebarNavCollapse = $('.fixed-sidebar-left .side-nav  li .collapse');
    var sidebarNavAnchor = '.fixed-sidebar-left .side-nav  li a';
    $(document).on("click",sidebarNavAnchor,function (e) {
        if ($(this).attr('aria-expanded') === "false")
            $(this).blur();
        $(sidebarNavCollapse).not($(this).parent().parent()).collapse('hide');
    });

    //下拉框响应
    $(document).on('click', '#toggle_nav_btn,#open_right_sidebar,#setting_panel_btn', function (e) {
        $(".dropdown.open > .dropdown-toggle").dropdown("toggle");
        return false;
    });
    $(document).off('click','#toggle_nav_btn').on('click', '#toggle_nav_btn', function (e) {
        $wrapper.removeClass('open-right-sidebar open-setting-panel').toggleClass('slide-nav-toggle');
        return false;
    });

    $(document).on('show.bs.dropdown', '.nav.navbar-right.top-nav .dropdown', function (e) {
        $wrapper.removeClass('open-right-sidebar open-setting-panel');
        return;
    });

    $(document).on('click', '#toggle_mobile_nav', function (e) {
        $wrapper.toggleClass('mobile-nav-open').removeClass('open-right-sidebar');
        return;
    });

    $(document).on("mouseenter mouseleave",".wrapper > .fixed-sidebar-left", function(e) {
        if (e.type == "mouseenter") {
            $wrapper.addClass("sidebar-hover");
        }
        else {
            $wrapper.removeClass("sidebar-hover");
        }
        return false;
    });

    $(document).on("mouseenter mouseleave",".wrapper > .setting-panel", function(e) {
        if (e.type == "mouseenter") {
            $wrapper.addClass("no-transition");
        }
        else {
            $wrapper.removeClass("no-transition");
        }
        return false;
    });

    //时间插件


    /*滚动条*/
    $('.nicescroll-bar').slimscroll({height:'100%',color: '#878787', disableFadeOut : true,borderRadius:0,size:'4px',alwaysVisible:false});
    $('.message-nicescroll-bar').slimscroll({height:'229px',size: '4px',color: '#878787',disableFadeOut : true,borderRadius:0});
    $('.product-nicescroll-bar').slimscroll({height:'346px',size: '4px',color: '#878787',disableFadeOut : true,borderRadius:0});
    $('.app-nicescroll-bar').slimscroll({height:'162px',size: '4px',color: '#878787',disableFadeOut : true,borderRadius:0});
};

var boxLayout = function() {
    if((!$wrapper.hasClass("rtl-layout"))&&($wrapper.hasClass("box-layout")))
        $(".box-layout .fixed-sidebar-right").css({right: $wrapper.offset().left + 300});
    else if($wrapper.hasClass("box-layout rtl-layout"))
        $(".box-layout .fixed-sidebar-right").css({left: $wrapper.offset().left});
}
boxLayout();

//初始化长度
$(window).on("resize", function () {
    setHeightWidth();
    boxLayout();
}).resize();

//重新获取dropdrown
var dropDrow={
    getDropDrown:function () {
        var dropdownSelectors = $('.dropdown, .dropup');
        dropdownSelectors.on({
            "show.bs.dropdown": function () {
                // On show, start in effect
                var dropdown = dropDrow.dropdownEffectData(this);
                dropDrow.dropdownEffectStart(dropdown, dropdown.effectIn);
            },
            "shown.bs.dropdown": function () {
                // On shown, remove in effect once complete
                var dropdown = dropDrow.dropdownEffectData(this);
                if (dropdown.effectIn && dropdown.effectOut) {
                    dropDrow.dropdownEffectEnd(dropdown, function() {});
                }
            },
            "hide.bs.dropdown":  function(e) {
                // On hide, start out effect
                var dropdown = dropDrow.dropdownEffectData(this);
                if (dropdown.effectOut) {
                    e.preventDefault();
                    dropDrow.dropdownEffectStart(dropdown, dropdown.effectOut);
                    dropDrow.dropdownEffectEnd(dropdown, function() {
                        dropdown.dropdown.removeClass('open');
                    });
                }
            },
        });
    },
    dropdownEffectData:function(target) {
        // @todo - page level global?
        var effectInDefault = null,
            effectOutDefault = null;
        var dropdown = $(target),
            dropdownMenu = $('.dropdown-menu', target);
        var parentUl = dropdown.parents('ul.nav');

        // If parent is ul.nav allow global effect settings
        if (parentUl.size() > 0) {
            effectInDefault = parentUl.data('dropdown-in') || null;
            effectOutDefault = parentUl.data('dropdown-out') || null;
        }

        return {
            target:       target,
            dropdown:     dropdown,
            dropdownMenu: dropdownMenu,
            effectIn:     dropdownMenu.data('dropdown-in') || effectInDefault,
            effectOut:    dropdownMenu.data('dropdown-out') || effectOutDefault,
        };
    },
    dropdownEffectStart:function(data, effectToStart) {
        if (effectToStart) {
            data.dropdown.addClass('dropdown-animating');
            data.dropdownMenu.addClass('animated');
            data.dropdownMenu.addClass(effectToStart);
        }
    },
    dropdownEffectEnd:function(data, callbackFunc) {
        var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
        data.dropdown.one(animationEnd, function() {
            data.dropdown.removeClass('dropdown-animating');
            data.dropdownMenu.removeClass('animated');
            data.dropdownMenu.removeClass(data.effectIn);
            data.dropdownMenu.removeClass(data.effectOut);

            // Custom callback option, used to remove open class in out effect
            if(typeof callbackFunc == 'function'){
                callbackFunc();
            }
        });
    }
};

